/*
 *  CELabelAtlas.cpp
 *  RedWeed
 *
 *  Created by Vasili Svirski on 05/11/12.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/labels/CELabelAtlas.h"



using namespace cocos2d;

namespace cocosext {

CELabelAtlas * CELabelAtlas::labelWithString(const wchar_t *label, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, wchar_t startCharMap)
	{
		CELabelAtlas *pRet = new CELabelAtlas();
		if(pRet && pRet->initWithString(label, charMapFile, itemWidth, itemHeight, startCharMap))
		{
			pRet->autorelease();
			return pRet;
		}
		CC_SAFE_DELETE(pRet)
		return NULL;
	}

bool CELabelAtlas::initWithString(const wchar_t *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, wchar_t startCharMap)
	{
		CCAssert(label != NULL, "");
		if (CCAtlasNode::initWithTileFile(charMapFile, itemWidth, itemHeight, wcslen(label)))
		{
			_mMapStartChar = startCharMap;
			this->setString(label);
			return true;
		}
		return false;
	}

void CELabelAtlas::setString(const wchar_t *label)
	{
		unsigned int len = wcslen(label);
		if (len > m_pTextureAtlas->getTotalQuads())
		{
			m_pTextureAtlas->resizeCapacity(len);
		}
		_mString.clear();
		_mString = label;
		this->updateAtlasValues();

		CCSize s;
		s.width = (float)(len * m_uItemWidth);
		s.height = (float)(m_uItemHeight);
		this->setContentSizeInPixels(s);

		m_uQuadsToDraw = len;
	}

void CELabelAtlas::updateAtlasValues()
	{
		unsigned int n = _mString.length();

		ccV3F_C4B_T2F_Quad quad;

		const  wchar_t *s = ( wchar_t*)_mString.c_str();

        CCTexture2D *texture = m_pTextureAtlas->getTexture();
        float textureWide = (float) texture->getPixelsWide();
        float textureHigh = (float) texture->getPixelsHigh();

		for(unsigned int i = 0; i < n; i++) {
			wchar_t a = s[i] - _mMapStartChar;
			float row = (float) (a % m_uItemsPerRow);
			float col = (float) (a / m_uItemsPerRow);

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
            // Issue #938. Don't use texStepX & texStepY
            float left		= (2 * row * m_uItemWidth + 1) / (2 * textureWide);
            float right		= left + (m_uItemWidth * 2 - 2) / (2 * textureWide);
            float top		= (2 * col * m_uItemHeight + 1) / (2 * textureHigh);
            float bottom	= top + (m_uItemHeight * 2 - 2) / (2 * textureHigh);
#else
            float left		= row * m_uItemWidth / textureWide;
            float right		= left + m_uItemWidth / textureWide;
            float top		= col * m_uItemHeight / textureHigh;
            float bottom	= top + m_uItemHeight / textureHigh;
#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

            quad.tl.texCoords.u = left;
            quad.tl.texCoords.v = top;
            quad.tr.texCoords.u = right;
            quad.tr.texCoords.v = top;
            quad.bl.texCoords.u = left;
            quad.bl.texCoords.v = bottom;
            quad.br.texCoords.u = right;
            quad.br.texCoords.v = bottom;

			quad.bl.vertices.x = (float) (i * m_uItemWidth);
			quad.bl.vertices.y = 0;
			quad.bl.vertices.z = 0.0f;
			quad.br.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth);
			quad.br.vertices.y = 0;
			quad.br.vertices.z = 0.0f;
			quad.tl.vertices.x = (float)(i * m_uItemWidth);
			quad.tl.vertices.y = (float)(m_uItemHeight);
			quad.tl.vertices.z = 0.0f;
			quad.tr.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth);
			quad.tr.vertices.y = (float)(m_uItemHeight);
			quad.tr.vertices.z = 0.0f;

			m_pTextureAtlas->updateQuad(&quad, i);
		}
	}
}
